package com.dao.impl;


import com.dao.SongerService;
import com.entity.Song;
import com.entity.Songer;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 歌手 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class SongerServiceImpl implements SongerService {


    @Override
    public int insert(Songer songer) throws SQLException {
       String sql ="insert into songer(songer_id,avater,sex,description,nickname,localtion,birthday,is_deleted,create_time,update_time) values(?,?,?,?,?,?,?,?,?,?) ";
        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{songer.getSongerId(),
                songer.getAvater(),songer.getSex(),songer.getDescription(),songer.getNickname(),songer.getLocaltion(),songer.getBirthday()
                ,0,new Date(),new Date()


        });
    }

    @Override
    public List<Songer> queryData(Songer songer) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from songer";
        List<Songer> songers = JdbcUtil.queryData(sql, Songer.class);
        return songers;
    }

    @Override
    public int update(Songer songer) throws SQLException {
        String sql = "update songer set avater=?,sex=?,description=?,nickname=?,localtion=?,birthday=?,is_deleted=?,create_time=?,update_time=? where songer_id=?";
        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                songer.getAvater(),songer.getSex(),songer.getDescription(),songer.getNickname(),songer.getLocaltion(),songer.getBirthday()
            ,songer.getIsDeleted(),songer.getCreateTime(),new Date(),songer.getSongerId()


        });
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql  = "update songer set is_deleted = 1 ,update_time = ? where songer_id=?";
        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{new Date(),id});
    }

    @Override
    public PageUtil<Songer> queryDataBypage(int currentPgae, int pageSize, Songer songer) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from songer where 1=1 and is_deleted=0 ";
        ArrayList values = new ArrayList();
        if (songer.getNickname()!=null&& !"".equals(songer.getNickname())){
            sql +="and nickname like ?";
            values.add("%"+songer.getNickname()+"%");
        }
        return JdbcUtil.queryDataByPage(sql, Songer.class,currentPgae,pageSize,values.toArray() );
    }

    @Override
    public int deletedIds(String[] ids) throws SQLException {
        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }
        String sql = "update songer set is_deleted = 1,update_time = ? where songer_id in (0";

        List<Object> list = new ArrayList<>();
        list.add(new Date());
        for (int i = 0; i < ids.length; i++) {
            sql += ",?";

            list.add(ids[i]);
        }

        sql += ")";

        int i = JdbcUtil.insertOrUpdateOrDelete(sql, list.toArray());

        return i;
    }
	/**
	 * 查询所有未删除的歌手
	 * @return
	 */
	@Override
	public List<Songer> queryListSong() throws SQLException, InstantiationException, IllegalAccessException {
		String sql = " select songer_id,nickname from songer where is_deleted = 0 ";
		return JdbcUtil.queryData(sql, Songer.class, null);
	}

	/**
	 * 根据id查询歌曲
	 * @param iId
	 * @return
	 */
	@Override
	public Song queryDataById(Integer iId) throws SQLException, InstantiationException, IllegalAccessException {
		String sql = "select * from song where song_id = ? ";
		List<Song> songs = JdbcUtil.queryData(sql, Song.class, new Object[]{iId});
		return songs.get(0);
	}
}
